<?php

namespace app\api\controller;


use app\common\controller\Api;
use app\api\model\coupon\CouponType as CouponTypeModel;

class Coupontype extends Api
{
    protected $noNeedRight = ['*'];


    //优惠券管理列表
    public function lists()
    {
        $list = CouponTypeModel::where(['user_id'=>$this->auth->id])
            ->order('createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('',$list);
    }


    //添加优惠券
    public function add()
    {
        $coupon_type_model = new CouponTypeModel;

        $data = [
            'user_id'=>$this->auth->id,
            'module'=>input('module','service'),
            'at_least'=>input('at_least',0),
            'money'=>input('money',0),
            'end_use_time'=>input('end_use_time',0),
        ];
        if (empty($data['end_use_time']))$this->error('请选择有效期限');
        if (empty($data['money']))$this->error('请输入优惠券金额');
        $res = $coupon_type_model->save($data);

        $this->success('success');
    }

    //编辑优惠券
    public function edit()
    {
        $coupon_type_id = input('coupon_type_id',0);

        $coupon_type_model = new CouponTypeModel;
        $data = [
            'user_id'=>$this->auth->id,
            'module'=>input('module','service'),
            'at_least'=>input('at_least',0),
            'money'=>input('money',0),
            'end_use_time'=>input('end_use_time',0),
        ];
        if (empty($data['end_use_time']))$this->error('请选择有效期限');
        if (empty($data['money']))$this->error('请输入优惠券金额');
        $res = $coupon_type_model->save($data,['coupon_type_id'=>$coupon_type_id]);
        $this->success('success');
    }

    //删除优惠券
    public function del()
    {
        $coupon_type_id = input('coupon_type_id',0);

        $coupon_type_info = CouponTypeModel::get($coupon_type_id);

        $coupon_type_info->delete();
        $this->success('success');
    }

    //优惠券信息
    public function info()
    {
        $coupon_type_id = input('coupon_type_id',0);

        $coupon_type_info = CouponTypeModel::get($coupon_type_id);


        $this->success('success',$coupon_type_info);
    }


}